package com.scalar.chiptrack.operations.wipflush.actioncommands;

import java.util.HashMap;
import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.db.DBXException;
import com.scalar.chiptrack.operations.wipflush.dao.WipFlushDetailDAO;
import com.scalar.chiptrack.operations.wipflush.forms.WipFlushSetupForm;
import com.scalar.chiptrack.users.ErrorReport;

/**
 * User: Sujan
 * Date: Nov 2, 2005
 * Time: 12:41:52 PM
 */
public class WipFlushSetupSaveCommand extends ActionCommand
{
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        WipFlushSetupForm wipFlushSetupForm = ( WipFlushSetupForm )form;
        HashMap info = (HashMap)request.getAttribute( INFO_MESSAGES );
        HashMap errors = ( HashMap )request.getAttribute( ERROR_MESSAGES );
        HttpSession session = request.getSession();

        DBTransaction dbTransaction = null;
        boolean success = false;

        try
        {
            dbTransaction = DBTransaction.getInstance();
            dbTransaction.begin();
            WipFlushDetailDAO.saveWipFlushSetup( dbTransaction, wipFlushSetupForm);
            //------------LOADING BASE PRODUCT FAMILY DETAILS---
            int baseProductId = wipFlushSetupForm.getBaseProductId();
            WipFlushDetailDAO.loadFormDetails( dbTransaction, wipFlushSetupForm );
            LinkedHashMap productFamilyDetailsMap = WipFlushDetailDAO.loadProductFamilyDetails( dbTransaction , baseProductId );
            wipFlushSetupForm.setWipFlushDetailsList( WipFlushDetailDAO.getWipFlushDetailsListFromMap(productFamilyDetailsMap));
            if ( productFamilyDetailsMap != null && productFamilyDetailsMap.size() > 0 )
            {
                request.setAttribute( "productFamilyDetailsMap", productFamilyDetailsMap );
            }
            else
            {
                System.out.println ( "====CouldNot find base product details map" );
                errors.put( "Could not find Base Product", null);
            }
            WipFlushDetailDAO.loadProcessStepBinDetails ( dbTransaction, wipFlushSetupForm );
            //------------END----
            info.put( "Wip Flush setup saved successfully", null );
            success = true;
        }
        catch( Exception e )
        {
            errors.put ( "Could not save Wip Flush setup", null );
            session.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while saving Wip Flush setup", e ) );
            e.printStackTrace();
        }
        finally
        {
            if ( dbTransaction != null )
            {
                try
                {
                    if ( success )
                    {
                        dbTransaction.commit();
                    }
                    else
                    {
                        dbTransaction.rollback();
                    }
                }
                catch ( DBXException e )
                {
                    e.printStackTrace();
                    System.out.println ( "--DBX Exception while committing/rollbacking in WipFlush Save command" );
                }
            }
        }
        return mapping.findForward( "saveSuccess" );
    }
}
